package com.dushibao.manage.settings;

import com.alibaba.fastjson2.JSON;
import com.dushibao.entity.User;
import com.dushibao.service.UserService;
import com.dushibao.service.impl.UserServiceImpl;
import com.dushibao.utils.SecurityHolder;
import com.dushibao.view.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/settings/editProfile")
public class EditProfileServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView("settings/editProfile");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader bw = req.getReader();
        String json = bw.readLine();

        User user = JSON.parseObject(json,User.class);
        user.setId(SecurityHolder.getCurrentUser().getId());
        Integer ret = userService.update(user);

        user = userService.getById(user.getId());

        SecurityHolder.setSessionUser(user);

        resp.getWriter().write(ret.toString());
    }
}
